import { _mallStore } from "@/store/config";

export const memberLevelsHandler = (row: ObjValueAny, i: number) => {
    if (+row.is_special === 1) {
        row.level = '特殊等级';
    } else {
        row.level = `等级${i + 1}`;
        // 升级条件 ===================================================
        let upgradeConditions: string = '';
        // level_up_condition 升级方式：0:无条件 1:积分 2:消费
        // 无条件
        if (row.level_up_condition === 0 && i === 0) {
            upgradeConditions = '注册';
        }
        // 积分
        if (+row.level_up_condition === 1) {
            const joinString: string = +row.point_relation === 1 ? '或' : '且';
            if (row.day_point_claim) {
                upgradeConditions = `单日累计获取${row.day_point_claim}积分`;
            }
            if (row.point_claim) {
                const _dayPointClaim: string = `累计获取${row.point_claim}积分`;
                if (upgradeConditions) {
                    upgradeConditions = `${upgradeConditions} ${joinString} ${_dayPointClaim}`;
                } else {
                    upgradeConditions = _dayPointClaim;
                }
            }
            if (row.direct_day_point_claim) {
                const _directDayPointClaim: string = `单日累计获取${row.direct_day_point_claim}积分`;
                if (upgradeConditions) {
                    upgradeConditions = `${upgradeConditions} ${joinString} ${_directDayPointClaim}`;
                } else {
                    upgradeConditions = _directDayPointClaim;
                }
            }
        }
        // 消费
        if (+row.level_up_condition === 2) {
            const joinString: string = +row.cost_relation === 1 ? '或' : '且';
            if (row.day_cost_claim) {
                upgradeConditions = `单日累计消费${row.day_cost_claim}元`;
            }
            if (row.year_cost_claim) {
                const _yearCostClaim: string = `累计消费${row.year_cost_claim}元`;
                if (upgradeConditions) {
                    upgradeConditions = `${upgradeConditions} ${joinString} ${_yearCostClaim}`;
                } else {
                    upgradeConditions = _yearCostClaim;
                }
            }
            if (row.direct_day_cost_claim) {
                const _directDayCostClaim: string = `单日累计消费${row.direct_day_cost_claim}元`;
                if (upgradeConditions) {
                    upgradeConditions = `${upgradeConditions} ${joinString} ${_directDayCostClaim}`;
                } else {
                    upgradeConditions = _directDayCostClaim;
                }
            }
        }
        row.upgradeConditions = (+row.level_up_condition === 1 || +row.level_up_condition === 2) && upgradeConditions ? `升级等级${i}后 ${upgradeConditions}` : upgradeConditions;

        // 保级条件 ===================================================
        let qualificationCriteria: string = '无';
        if (+row.is_effective !== 0) {
            if (row.point_retain) {
                qualificationCriteria = `获取${row.point_retain}积分`;
            }
            if (row.cost_retain) {
                qualificationCriteria = `消费${row.cost_retain}元`;
            }
        }
        if (i == 0 || +row.is_special === 1) {
            qualificationCriteria = '';
        }
        row.qualificationCriteria = +row.is_effective !== 0 && qualificationCriteria ? `有效期内累计${qualificationCriteria}` : qualificationCriteria;
    }

    // 升级至本等级条件 ===================================================
    if (row.is_effective === 0) {
        row.effectiveDays = '永久有效';
    } else if (row.is_effective === 1 && +row.effective_days === 0) {
        row.effectiveDays = '当天有效';
    } else {
        row.effectiveDays = `${row.effective_days}天`;
    }
    // 会员卡卡面 card_style 0:默认 1:自定义样式 ===================================================
    if (+row.card_style !== 1) {
        if (+_mallStore.mallZeusConfig?.miniapp?.customize_template === 1) {
            row.card_img_url = 'https://cdn.zhimatech.com/bg_black.png';
        } else {
            row.card_img_url = 'https://cdn.zhimatech.com/bg_red.png';
        }
    }
}